package yxy.game.pm2.module.camptower;

import org.joda.time.DateTime;
import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.module.battle.BattleModule;
import yxy.game.pm2.module.battle.BattleRecord;

/**
 * @ClassName CampTowerFloorModule
 * @Author jingtao-zheng
 * @Date 2022/8/2 11:54
 * @Version 1.0
 **/
public class CampTowerFloorModule extends AreaModule {
    // bean===========================================================================
    private int floor;//层
    private CampTowerFloorRecord bestRound;// 最少回合
    private CampTowerFloorRecord bestPower;// 最小战力
    private int type;//阵营塔类型

    public int getFloor() {
        return floor;
    }

    public void setFloor(int floor) {
        this.floor = floor;
    }

    public CampTowerFloorRecord getBestRound() {
        return bestRound;
    }

    public void setBestRound(CampTowerFloorRecord bestRound) {
        this.bestRound = bestRound;
    }

    public CampTowerFloorRecord getBestPower() {
        return bestPower;
    }

    public void setBestPower(CampTowerFloorRecord bestPower) {
        this.bestPower = bestPower;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
    // func===========================================================================

    public void record(String playerCode, BattleModule module,int type) {
        try {
            if (tryLock()) {
                //记录最少轮数通关
                recordBestRound(playerCode, module,type);
                //记录最少战力通关
                recordBestPower(playerCode, module,type);
            }
        } catch (Exception e) {
        } finally {
            unlock();
        }
    }

    private void recordBestRound(String playerCode, BattleModule module, int type) {
        long round = module.getRound();
        CampTowerFloorRecord record = getBestRound();
        if (record == null) {
//			String recordCode = BattleRecord.randomCode();
            String recordCode = String.format("campBestRoundFloor%s:%s:%s", getFloor(), module.getPlayerCode(),type);
            BattleRecord battleRecord = module.saveRecord(recordCode);
            record = new CampTowerFloorRecord();
            record.setPlayerCode(playerCode);
            record.setDate(DateTime.now());
            record.setValue(round);
            record.setRecordCode(battleRecord.getCode());
            setBestRound(record);
            set();
            return;
        }
        if (record.getValue() < round) {
            return;
        }
        String recordCode = record.getRecordCode();
        module.saveRecord(recordCode);
        record.setPlayerCode(playerCode);
        record.setValue(round);
        record.setDate(DateTime.now());
        set();
    }

    private void recordBestPower(String playerCode, BattleModule module,int type) {
        long power = module.getTeamA().getPower();
        CampTowerFloorRecord record = getBestPower();
        if (record == null) {
//			String recordCode = BattleRecord.randomCode();
            String recordCode = String.format("towerBestPowerFloor%s:%s:%s", getFloor(), module.getPlayerCode(),type);
            BattleRecord battleRecord = module.saveRecord(recordCode);
            record = new CampTowerFloorRecord();
            record.setPlayerCode(playerCode);
            record.setDate(DateTime.now());
            record.setValue(power);
            record.setRecordCode(battleRecord.getCode());
            setBestPower(record);
            set();
            return;
        }
        if (record.getValue() < power) {
            return;
        }
        String recordCode = record.getRecordCode();
        module.saveRecord(recordCode);
        record.setPlayerCode(playerCode);
        record.setValue(power);
        record.setDate(DateTime.now());
        set();
    }


    // redis==========================================================================

    static public String key(int areaId,int type) {
        return key(areaId, String.format("CAMPTOWERFLOOR:%s",type));
    }

    static private RMap<Integer, CampTowerFloorModule> touchMap(int areaId,int type) throws RedisException {
        return RedisManager.client(redis()).getMap(key(areaId,type));
    }

    public void set() {
        try {
            RMap<Integer, CampTowerFloorModule> map = touchMap(getAreaId(),getType());
            map.put(getFloor(), this);
        } catch (RedisException e) {
        }
    }

    static public CampTowerFloorModule get(int areaId, int floor,int type) {
        try {
            RMap<Integer, CampTowerFloorModule> map = touchMap(areaId,type);
            if (!map.containsKey(floor)) {
                CampTowerFloorModule module = create(areaId, floor,type);
                map.put(floor, module);
            }
            return map.get(floor);
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static CampTowerFloorModule create(int areaId, int floor,int type) {
        CampTowerFloorModule module = new CampTowerFloorModule();
        module.setFloor(floor);
        module.setAreaId(areaId);
        module.setType(type);
        return module;
    }

    public static CampTowerFloorModule get(String playerCode, int floor,int type) {
        try {
            AreaBean area = area(playerCode);
            return CampTowerFloorModule.get(area.getAreaId(), floor,type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // lock==========================================================================
    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getAreaId(),getType()));
    }
}
